package com.ie.sevice.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.MyUtil;
import com.ie.entity.CartEntity;
import com.ie.entity.FocusEntity;
import com.ie.entity.GoodsEntity;
import com.ie.mapper.admin.GoodsMapper;
import com.ie.sevice.before.FocusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname: GoodsServiceImpl
 * @Author: S2
 * @Description: //TODO
 * @Date: 2025/5/12  14:46
 * @Param:
 * @version: 1.0
 **/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private FocusService focusService;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        Integer pnum = goodsEntity.getCurrentPage();
        if (pnum == null || pnum < 1) {
            goodsEntity.setCurrentPage(1);
        }
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if (goodstypeId != null && goodstypeId > 0) {
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }
        if (gname != null && !gname.isEmpty()) {
            lambdaQueryWrapper.like(GoodsEntity::getGname, gname);
        }
        // 查询条件可以更复杂
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(iPage, lambdaQueryWrapper);
        HashMap<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> listAdv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId).last("limit 5").list();
        return ResponseResult.getSuccessResult(listAdv);
    }

    @Override
    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {
        // return ResponseResult.getSuccessResult(goodsMapper.selectById(goodsEntity.getId()));
        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if (goodstypeId != null && goodstypeId > 0) {
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }
        if (gname != null && !gname.isEmpty()) {
            String trimgname = gname.trim();
            lambdaQueryWrapper.like(GoodsEntity::getGname, trimgname);
        }
        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        List<GoodsEntity> listindex = list(lambdaQueryWrapper);
        return ResponseResult.getSuccessResult(listindex);
    }

    @Override
    public ResponseResult<Map<String, String>> addGoods(GoodsEntity goodsEntity) {
        byte[] logoFile = goodsEntity.getLogoFile();
        if (logoFile != null && logoFile.length > 0) {
            // Vue前端静态资源的路径,这个要修改的
            // 考虑OS
            // 标胶好的解决OSS
            String path = "E:\\Vue_Project\\ebusiness-vue\\src\\assets";
            // String path_linux = "/var/www/ebusiness-vue/src/assets";
            String fileName = MyUtil.getNewFileName(goodsEntity.getFileName());
            File filePath = new File(path + File.separator + fileName);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                fileOutputStream.write(logoFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            goodsEntity.setGpicture(fileName);
        }
        if ("add".equals(goodsEntity.getAct())) {
            boolean result = save(goodsEntity);
            if (result) {
                return ResponseResult.getMessageResult(null, "A001");
            } else {
                return ResponseResult.getMessageResult(null, "A002");
            }

        } else {
            boolean result = updateById(goodsEntity);
            if (result) {
                return ResponseResult.getMessageResult(null, "A001");
            } else {
                return ResponseResult.getMessageResult(null, "A002");
            }
        }
    }

    @Override
    public ResponseResult<Map<String, String>> deleteGoods(GoodsEntity goodsEntity) {
        // TODO
        long res1 = 0;
        // TODO
        long res2 = 0;
        // TODO
        long res3 = 0;
        // 不能删除
        if (res1 > 0 || res2 > 0 || res3 > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        if (removeById(goodsEntity)) {
            return ResponseResult.getMessageResult(null, "A002");
        } else {
            return ResponseResult.getMessageResult(null, "A003");
        }
        // return null;
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCarGoods(CartEntity cartEntity) {
        List<Map<String, Object>> list = goodsMapper.myCartGoods(cartEntity.getBusertableId());
        return ResponseResult.getSuccessResult(list);
    }

    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        IPage<GoodsEntity> igae = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<FocusEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FocusEntity::getBusertableId, goodsEntity.getBusertableId());
        IPage<GoodsEntity> page = goodsMapper.iPageMyFocusGoods(igae, lambdaQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("allGoods", page.getRecords());
        map.put("total", page.getTotal());
        return ResponseResult.getSuccessResult(map);
    }

}
